package com.cl.ks.controller;


import com.alibaba.fastjson.JSONObject;
import com.cl.ks.entity.KsFlowGraph;
import com.cl.ks.flow.KsFlowGraphHandler;
import com.cl.ks.flow.util.GJ2SQLUtil;
import com.cl.ks.service.KsFlowGraphService;
import com.cl.ks.utils.CommonResult;
import com.cl.mvc.controller.BaseController;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("KsFlowGraph")
public class KsFlowGraphController extends BaseController<KsFlowGraph> {
    private final KsFlowGraphService ksFlowGraphService;

    public KsFlowGraphController(KsFlowGraphService service, KsFlowGraphService ksFlowGraphService) {
        super(service);
        this.ksFlowGraphService = ksFlowGraphService;
    }

    @PostMapping("gjJsonToSQL")
    public CommonResult gjJsonToSQL(@RequestBody String gjJson) {
        return CommonResult.success(GJ2SQLUtil.gjToSQL(gjJson));
    }

    @RequestMapping(path = "removeKsFlowGraph", method = RequestMethod.GET)
    public CommonResult<JSONObject> removeKsFlowGraph(@RequestParam("id") Integer id, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        KsFlowGraphHandler.removeKsFlowGraphRunning(id);
        return CommonResult.success(null,"success");
    }

    @PostMapping("cleanCache")
    public CommonResult cleanCache(@RequestBody KsFlowGraph entity, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        try {
            ksFlowGraphService.cleanCache(entity,auth);
            return CommonResult.success("成功");
        } catch (Exception e) {
            return CommonResult.failed(e.getMessage());
        }
    }

    @RequestMapping(path = "/v1/deleteById", method = RequestMethod.GET)
    public CommonResult<JSONObject> deleteById(@RequestParam("id") Integer id, @RequestParam("deleteCreateTablesFlag") Boolean deleteCreateTablesFlag, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        ksFlowGraphService.deleteById(id, deleteCreateTablesFlag);
        return CommonResult.success(null,"success");
    }

    @RequestMapping(path = "/tagsGroupCount", method = RequestMethod.GET)
    public CommonResult<JSONObject> tagsGroupCount(@RequestParam("tags") List<String> tags, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        return CommonResult.success(ksFlowGraphService.tagsGroupCount(tags, auth));
    }

    @RequestMapping(path = "/exportKsFlowGraphs", method = RequestMethod.GET)
    public void exportKsFlowGraphs(@RequestParam("ids") List<Integer> ids, HttpServletResponse response) {
        ksFlowGraphService.exportKsFlowGraphs(ids, response);
    }

    @RequestMapping(path = "/exportKsFlowGraphsJson", method = RequestMethod.GET)
    public JSONObject exportKsFlowGraphsJson(@RequestParam("ids") List<Integer> ids, HttpServletResponse response) {
        return ksFlowGraphService.exportKsFlowGraphsJson(ids, response);
    }


    @RequestMapping(path = "/importKsFlowGraphs", method = RequestMethod.POST)
    public CommonResult importKsFlowGraphs(@RequestParam("file") MultipartFile file, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        try {
            ksFlowGraphService.importKsFlowGraphs(file, auth);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CommonResult.success("", "导入成功");
    }


    @RequestMapping(path = "/importKsFlowGraphsJSON", method = RequestMethod.POST)
    public void importKsFlowGraphsJSON(@RequestBody String ksFlowGraphsJson, @RequestHeader(name = "Authorization", defaultValue = "") String auth) {
        ksFlowGraphService.importKsFlowGraphsJson(ksFlowGraphsJson, auth);
    }
}
